package io.geekidea.boot.config;

import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import java.time.Duration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RateLimitConfig {

  @Bean
  public RateLimiter rateLimiterCustom() {
    RateLimiterConfig config = RateLimiterConfig.custom()
        .limitForPeriod(3)               // 每个时间窗口允许的请求数
        .limitRefreshPeriod(Duration.ofSeconds(1)) // 时间窗口大小
        .timeoutDuration(Duration.ofMillis(1000))     // 获取令牌最大等待时间
        .build();

    return RateLimiter.of("default", config);
  }

}
